package com.xnx3.wangmarket.admin.controller;

import com.xnx3.j2ee.func.AttachmentFile;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.j2ee.vo.UploadFileVO;
import com.xnx3.wangmarket.Utils.FileUtil;
import com.xnx3.wangmarket.Utils.ServerAddressUtils;
import com.xnx3.wangmarket.admin.entity.EmpiricalConclusion;
import com.xnx3.wangmarket.admin.entity.Page;
import com.xnx3.wangmarket.admin.entity.Product;
import com.xnx3.wangmarket.admin.entity.SiteColumn;
import com.xnx3.wangmarket.admin.service.EmpiricalConclusionService;
import com.xnx3.wangmarket.admin.service.ProductManagementService;
import com.xnx3.wangmarket.admin.util.AliyunLog;
import com.xnx3.wangmarket.admin.vo.EmpiricalConclusionVo;
import com.xnx3.wangmarket.admin.vo.bean.ProductVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xnx3.j2ee.shiro.ShiroFunc.getUserId;

@Controller
@RequestMapping("/EmpiricalController")
public class EmpiricalController {

    @Resource
    private SqlService sqlService;

    @Resource
    private EmpiricalConclusionService empirica;
    String empiricalImg;

    @RequestMapping("/queryEmpiricalList.do")
    public String queryEmpiricalList(Model model, HttpServletRequest request){
        
        List<SiteColumn> site = empirica.empiricalType(getUserId());
        EmpiricalConclusion empirical = new EmpiricalConclusion();
        String empiricalName = request.getParameter("empiricalName");
        String empiricalType = request.getParameter("empiricalType");
        if (empiricalName!=null && empiricalType!=null&&!empiricalName.isEmpty()&&!empiricalType.isEmpty()) {
            empirical.setName(empiricalName);
            empirical.setSiteColumnId(Integer.parseInt(empiricalType));
        }
        if (empiricalName!=null&&!empiricalName.isEmpty()) {
            empirical.setName(empiricalName);
        }
        if (empiricalType!=null&&!empiricalType.isEmpty()) {
            empirical.setSiteColumnId(Integer.parseInt(empiricalType));
        }

        int currentPage = 1;
        int countPage = 10;
        String value = request.getParameter("page");
        if (value != null && !"".equals(value)) {
            currentPage = Integer.parseInt(value);
        }
        Page pg = empirica.findPage(currentPage, countPage, empirical);

        Map<String,Object> map = new HashMap<>();
        map.put("type",site);
        map.put("ecList",pg);

        model.addAttribute("type", site);
        model.addAttribute("ecList", pg);
        return "site/experimentManagement";
    }

    @RequestMapping("/experimentList.do")
    @ResponseBody
    public Map<String,Object> experimentList(HttpServletRequest request) {
        List<SiteColumn> site = empirica.empiricalType(getUserId());
        String page = request.getParameter("page");
        EmpiricalConclusion ect = new EmpiricalConclusion();
        String ectName = request.getParameter("ectName");
        String ectType = request.getParameter("type");
        if (ectName!=null && ectType!=null&&!ectName.isEmpty()&&!ectType.isEmpty()) {
            ect.setName(ectName);
            ect.setSiteColumnId(Integer.parseInt(ectType));
        }
        if (ectName!=null&&!ectName.isEmpty()) {
            ect.setName(ectName);
        }
        if (ectType!=null&&!ectType.isEmpty()) {
            ect.setSiteColumnId(Integer.parseInt(ectType));
        }
        int currentPage = 1;
        int countPage = 10;

        if (page != null && !"".equals(page)) {
            currentPage = Integer.parseInt(page);
        }
        Map<String,Object> map = new HashMap<>();
        Page pg = empirica.findPage(currentPage, countPage, ect);
        map.put("type",site);
        map.put("pctList",pg);
        return map;
    }
    //新增产品数据获取
    @RequestMapping("/empiricalAddList.do")
    public String empiricalAddList(Model model, HttpServletRequest request) {

        if (request.getParameter("id")!=null && !"".equals(request.getParameter("id"))) {
            String id = request.getParameter("id");
            if(Integer.parseInt(id)>0){
                EmpiricalConclusion  ect = empirica.queryEmpirical(Integer.parseInt(id));
                model.addAttribute("ect",ect);
            }
        }
        List<SiteColumn> site = empirica.empiricalType(getUserId());
        model.addAttribute("siteType", site);

        return "site/experimentAdd";
    }
//添加和修改实验
    @RequestMapping("/empiricalAdd.do")
    @ResponseBody
    public EmpiricalConclusionVo empiricalAdd(EmpiricalConclusion ect, HttpServletRequest request, Model model) {

        Integer id = Integer.parseInt(request.getParameter("product_type"));
        EmpiricalConclusionVo empiricalConclusionVo = new EmpiricalConclusionVo();
        SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = sdft.format(new Date().getTime());
        EmpiricalConclusion pct = new EmpiricalConclusion();

        if (!request.getParameter("id").equals("")) {
            Integer cid = Integer.parseInt(request.getParameter("id"));
            if (cid > 0) {
                EmpiricalConclusion prod = sqlService.findById(EmpiricalConclusion.class, cid);

                if (prod.getId() == cid) {

                    if (empiricalImg != null&&!"".equals(empiricalImg)) {
                        prod.setImgPath(empiricalImg);
                    }
                    if (ect.getName() != null) {
                        prod.setName(ect.getName());
                    }
                    if (ect.getEmpiricalConclusion() != null) {
                        prod.setEmpiricalConclusion(ect.getEmpiricalConclusion());
                    }
                    if (ect.getProcessNarration() != null) {
                        prod.setProcessNarration(ect.getProcessNarration());
                    }
                    prod.setModifyTime(dateTime);
                    prod.setSiteColumnId(id);
                    prod.setUserId(getUserId());
                    empirica.updateEmpirical(prod, cid, request);
                    model.addAttribute("name",ect.getName());
                    empiricalConclusionVo.setBaseVO(BaseVO.SUCCESS, prod.getId() > 0 ? "保存成功！" : "创建成功！");
                    empiricalConclusionVo.setEmpiricalConclusion(pct);
                }
            }
        } else {
            SiteColumn column = empirica.pctType(id);
            if (ect.getEmpiricalConclusion() == null || ect.getName() == null) {
                empiricalConclusionVo.setBaseVO(BaseVO.FAILURE, "保存失败！");
                empiricalConclusionVo.setEmpiricalConclusion(pct);
            } else {
                if (column.getId() > 0 && id > 0) {

                    pct.setImgPath(empiricalImg);
                    pct.setModifyTime(dateTime);
                    pct.setProcessNarration(ect.getProcessNarration());
                    pct.setEmpiricalConclusion(ect.getEmpiricalConclusion());
                    pct.setSiteColumnId(ect.getSiteColumnId());
                    pct.setTimestamps(dateTime);
                    pct.setUserId(getUserId());
                    pct.setSiteColumnId(id);
                    pct.setName(ect.getName());
                    empirica.updateEmpirical(pct, getUserId(), request);
                    empiricalConclusionVo.setBaseVO(BaseVO.SUCCESS, pct.getId() > 0 ? "保存成功！" : "创建成功！");
                    empiricalConclusionVo.setEmpiricalConclusion(pct);
                }
            }
        }
        empiricalImg="";
        return empiricalConclusionVo;
    }

    /**
     * 删除实验信息
     ***/
    @RequestMapping("/delEmpirical.do")
    @ResponseBody
    public EmpiricalConclusionVo delEmpirical(HttpServletRequest request) {

        EmpiricalConclusionVo productVO = new EmpiricalConclusionVo();
        String id = request.getParameter("id");
        if(id!=null&&!"".equals(id)){
            EmpiricalConclusion pct = new EmpiricalConclusion();
            pct.setId(Integer.parseInt(id));
            EmpiricalConclusion empiri = sqlService.findById(EmpiricalConclusion.class,Integer.parseInt(id));
            FileUtil.deleteFile(ServerAddressUtils.tomcatPath(request)+empiri.getImgPath());

            empirica.delEmpirical(pct);
            productVO.setBaseVO(BaseVO.SUCCESS, Integer.parseInt(id) > 0 ? "保存成功！" : "创建成功！");
            productVO.setEmpiricalConclusion(pct);
        }else{
            productVO.setBaseVO(BaseVO.FAILURE,"删除失败！");
        }
        return productVO;
    }

    /**
     * 上传轮播图片接口
     */
    @RequestMapping(value = "uploadEmpiricalImg${url.suffix}", method = RequestMethod.POST)
    @ResponseBody
    public UploadFileVO uploadProductImg(Model model, HttpServletRequest request) {
        UploadFileVO uploadFileVO = AttachmentFile.uploadImage("site/" + getUserId() + "/news/empirical/", request, "file", 0);
        if (uploadFileVO.getResult() == UploadFileVO.SUCCESS) {
            //上传成功，写日志
            empiricalImg = uploadFileVO.getPath();
            AliyunLog.addActionLog(getUserId(), "内容管理上传图片成功：" + uploadFileVO.getFileName(), uploadFileVO.getPath());
        }
        return uploadFileVO;
    }

}
